class Solution {
public:
    vector<int> diStringMatch(string s) {
        int n = s.size();
        vector<int> idx(n + 1);
        for(int i = 0; i <= n; i++)
            idx[i] = i;
        
        vector<int> ret(n + 1);
        int left = 0, right = n;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == 'I')
                ret[i] = idx[left++];
            else
                ret[i] = idx[right--];
        }
        ret[n] = s[n - 1] == 'I' ? idx[left] : idx[right];
        return ret;
    }
};